package com.aplink.generic.xmlparser;

import android.content.Context;
import android.content.res.Resources;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class XMLPaserUtil {

    private static Document buildDocument( InputStream istream )
            throws ParserConfigurationException, SAXException, IOException {
        final InputSource isource = new InputSource( new InputStreamReader(
                istream, "UTF-8" ) );

        final DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                .newInstance();
        final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        final Document doc = dBuilder.parse( isource );
        doc.getDocumentElement().normalize();
        return doc;
    }

    /**
     * Open XML document from Raw folder
     *
     * @param context
     * @param xmlIdFile
     * @return
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public static Document openDocumentFrom( Context context, int xmlIdFile )
            throws ParserConfigurationException, SAXException, IOException,
            Resources.NotFoundException {
        final InputStream istream = context.getResources().openRawResource(
                xmlIdFile );
        return buildDocument( istream );
    }

    /**
     * Open XML document from Asset folder.
     *
     * @param context
     * @param xmlFileName
     * @return
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public static Document openDocumentFrom( Context context, String xmlFileName )
            throws ParserConfigurationException, SAXException, IOException {
        final InputStream istream = context.getAssets().open( xmlFileName );
        return buildDocument( istream );
    }

    public static String getAttrValue( Node node, String attrName ) {
        Attr attr = (Attr) node.getAttributes().getNamedItem( attrName );
        if ( attr != null ) {
            return attr.getValue();
        }
        return null;
    }

    public static String getNodeValue( Node node ) {
        Node child;
        if ( node != null ) {
            if ( node.hasChildNodes() ) {
                child = node.getFirstChild();
                while ( child != null ) {
                    if ( child.getNodeType() == Node.TEXT_NODE ) {
                        return child.getNodeValue();
                    }
                    child = child.getNextSibling();
                }
            }
        }
        return null;
    }
}
